﻿package {
	import flash.display.*;
	import flash.geom.Matrix;
	import flash.events.Event;
	import flash.events.MouseEvent;
			
	public class ToggleButton extends Sprite {
		public var scale: Number = 30;
		public var matrix:Matrix = new Matrix;
		public var red: Boolean = false;
		public var _up: Boolean = true;
		public var go: Boolean;
	 	public var gradcolors:Array = new Array;
 		public var linecolors:Array = new Array;
		private var _height:Number=100;
	
		public function ToggleButton(height:Number=100) 
		{
			_height = height;
			init();
		}
		
		public function init():void
		{
			scale = .1 * _height;
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			draw(scale,_up,red);
		}
		
		
		private function onMouseOver(event:MouseEvent):void
		{
			red = true;
			draw(scale,_up,red);

		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			red = false;
			draw(scale,_up,red);

		}

		private function onMouseDown(event:MouseEvent):void
		{

			if (_up) {
				_up = false;
			}
			else {
				_up = true;
			}
			draw(scale,_up,red);
		}
		
		private function onMouseUp(event:MouseEvent):void
		{
			
		}

		public function draw(scale:Number, _up:Boolean, red:Boolean):void
		{
			matrix.createGradientBox(0.95*scale, 0.8*scale, Math.PI/2,  0, 0);
			
			if(_up)
			{
				gradcolors = [0xffffff, 0x777777];
				linecolors = [0x777777, 0xffffff];	
			}
			else
			{
				gradcolors = [0x777777, 0xffffff];	
				linecolors = [0xffffff, 0x777777];
			}
			graphics.lineStyle(1,0x000000,0);
			graphics.beginGradientFill(GradientType.LINEAR, gradcolors, [1,1], [10,245], matrix);
			graphics.drawRoundRect(0,0,.95*scale,.8*scale,.3*scale,.3*scale);
			graphics.endFill();
						
			matrix.createGradientBox(0.45*scale, 0.5*scale, Math.PI/2, 0.3*scale, 0.15*scale);
			
						
			if(red)
			{
				graphics.beginGradientFill(GradientType.LINEAR, [0xee0000, 0x770000], 
			[1,1], [0,255], matrix);
			}
			else
			{
				graphics.beginGradientFill(GradientType.LINEAR, [0xdddddd, 0x000000], 
			[1,1], [0,255], matrix);
			}
			graphics.lineStyle(1,0x000000,.8);
			graphics.drawCircle(.475*scale,.4*scale,.25*scale);
			graphics.endFill();			
			
	}
		public function get up():Boolean
		{
			return _up;
		}
			
	}
						
}
		
		

